<template>
	<view class="content">
		<u-form ref="uForm" label-width="200" class="formStyle">
			<view class="flexFull">
				<view class="oneCard">
					<view class="line-view">
						<u-input
							border
							v-model="goodsCode"
							:focus="canFocus"
							:inputBorder="false"
							@focus="goodsCode = ''"
							placeholder="扫描上架物料"
							@confirm="confirmGoods()"
						></u-input>
					</view>
					<view class="line-view">
						<u-input border v-model="scanSearchText" :inputBorder="false" @focus="scanSearchText = ''" placeholder="扫描仓位" @confirm="confirmOne()"></u-input>
					</view>
					<u-form-item label="仓位:" v-if="stockObj && stockObj.name">{{ stockObj.name }}</u-form-item>
				</view>
				<view v-for="(item, index) in formModel" :key="index" class="oneCard">
					<u-form-item label="物料代码:">{{ item.productCode }}</u-form-item>
					<u-form-item label="物料名称:">{{ item.productName }}</u-form-item>
					<u-form-item label="规格型号:">{{ item.productModel }}</u-form-item>
					<!-- 同种物料不同批次，下面循环 -->
					<view v-for="(itemLit, indexLit) in item.innerList" :key="indexLit" class="oneLit">
						<u-form-item label="批号:" label-width="130">{{ itemLit.batch }}</u-form-item>
						<u-form-item label="数量:" label-width="130">{{ itemLit.quality }}</u-form-item>
					</view>
					<!-- 同种物料不同批次，下面循环 -->
				</view>
			</view>
		</u-form>
		<!-- <view class="btnRow"><u-button type="primary" size="medium" @click="subAll">上架</u-button></view> -->
		<view style="padding: 25rpx;"><button type="primary" @click="subAll">上 架</button></view>
		<u-toast ref="uToast" />
	</view>
</template>

<script>
export default {
	data() {
		return {
			canFocus: false,
			scanSearchText: '',
			stockObj: {},
			goodsCode: '',
			formModel: []
		};
	},
	mounted() {
		this.canFocus = true;
	},
	methods: {
		subAll() {
			let listClone = JSON.parse(JSON.stringify(this.formModel));
			if (!this.stockObj.id) {
				this.$refs.uToast.show({
					title: '还没有扫描仓位！',
					type: 'error'
				});
				return;
			}
			if (listClone.length == 0) {
				this.$refs.uToast.show({
					title: '请扫描货物！',
					type: 'error'
				});
				return;
			}
			let listPar = [];
			listClone.forEach(item => {
				item.innerList.forEach(itemp => {
					listPar.push(itemp);
				});
			});

			this.$u.post('/wms/mobile/api/goodsUpBatch', { stockPositionId: this.stockObj.id, goodsUpRecordSaveRequests: listPar }).then(res => {
				if (res.code != 0) {
					this.$refs.uToast.show({
						title: res.message,
						type: 'error'
					});
					return;
				}
				this.$refs.uToast.show({
					title: '货物上架成功！',
					type: 'success',
					callback: () => {
						uni.navigateBack({
							delta: 1
						});
					}
				});
			});
		},
		confirmOne() {
			// 根据仓位code查询库存
			if (!this.scanSearchText) {
				return;
			}
			this.$u.get('/wms/mobile/api/getStockPalace', { code: this.scanSearchText }).then(res => {
				if (res.code != 0) {
					this.$refs.uToast.show({
						title: res.message,
						type: 'error'
					});
					return;
				}
				this.stockObj = res.data;
			});
		},
		confirmGoods() {
			this.canFocus = false;
			// 扫描要上架的物料
			if (!this.goodsCode) {
				return;
			}
			let canChange = true;
			let formModelClone = this.formModel;
			formModelClone.forEach(item => {
				item.innerList.forEach(itemOne => {
					if (itemOne.batch == this.goodsCode) {
						canChange = false;
					}
				});
			});
			if (!canChange) {
				this.$refs.uToast.show({
					title: '此物料已存在，不可重复',
					type: 'error'
				});
				return;
			}
			this.$u.get('/wms/mobile/api/queryOffShelfMaterial', { barcode: this.goodsCode }).then(res => {
				this.canFocus = true;
				if (res.code != 0) {
					this.$refs.uToast.show({
						title: res.message,
						type: 'error'
					});
					return;
				}
				if (this.formModel == 0) {
					let obj = {
						productCode: JSON.parse(JSON.stringify(res.data.productCode)),
						productModel: JSON.parse(JSON.stringify(res.data.productModel)),
						productName: JSON.parse(JSON.stringify(res.data.productName)),
						innerList: [res.data]
					};
					let list = [obj];
					this.formModel = JSON.parse(JSON.stringify(list));
				} else {
					let joinCan = false;
					let listIndex = null;
					this.formModel.forEach((item, index) => {
						if (item.productCode == res.data.productCode) {
							listIndex = index;
							joinCan = true;
						}
					});
					if (joinCan) {
						let listClone = JSON.parse(JSON.stringify(this.formModel));
						listClone[listIndex].innerList.push(res.data);
						this.formModel = JSON.parse(JSON.stringify(listClone));
					} else {
						let listClone = JSON.parse(JSON.stringify(this.formModel));
						let obj = {
							productCode: JSON.parse(JSON.stringify(res.data.productCode)),
							productModel: JSON.parse(JSON.stringify(res.data.productModel)),
							productName: JSON.parse(JSON.stringify(res.data.productName)),
							innerList: [res.data]
						};
						listClone.push(obj);
						this.formModel = JSON.parse(JSON.stringify(listClone));
					}
				}
			});
		}
	}
};
</script>

<style lang="scss">
page {
	background-color: #f8f8f8;
}
.content {
	height: 100%;
}

.oneCard {
	background-color: #fff;
	border-radius: 10rpx;
	padding: 25rpx;
}

.btnRow {
	height: 80rpx;
	display: flex;
	justify-content: center;
	align-items: center;
}

.u-form-item {
	padding: 0 0;
}

.formStyle {
	display: flex;
	flex-flow: column;
	height: calc(100% - 80rpx);
}

.oneLit {
	padding: 0 0 0 20rpx;
}

.flexFull {
	flex: 1;
	overflow-y: auto;
}
</style>
